package com.example.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class AdminLoginFrame extends JFrame {
    // 管理员登录界面实现
    public AdminLoginFrame() {
        setTitle("管理员登录");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel(new GridLayout(4, 1, 10, 10));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 标题
        JLabel titleLabel = new JLabel("管理员登录", JLabel.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
        panel.add(titleLabel);

        // 用户名输入
        JPanel userPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel userLabel = new JLabel("管理员账号:");
        userLabel.setFont(new Font("宋体", Font.PLAIN, 16));
        JTextField userField = new JTextField(15);
        userField.setFont(new Font("宋体", Font.PLAIN, 16));
        userPanel.add(userLabel);
        userPanel.add(userField);
        panel.add(userPanel);

        // 密码输入
        JPanel passPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel passLabel = new JLabel("密      码:");
        passLabel.setFont(new Font("宋体", Font.PLAIN, 16));
        JPasswordField passField = new JPasswordField(15);
        passField.setFont(new Font("宋体", Font.PLAIN, 16));
        passPanel.add(passLabel);
        passPanel.add(passField);
        panel.add(passPanel);

        // 登录按钮
        JButton loginButton = new JButton("登  录");
        loginButton.setFont(new Font("宋体", Font.BOLD, 16));
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 这里添加管理员登录验证逻辑
                String username = userField.getText();
                char[] password = passField.getPassword();

                // 示例验证 - 实际项目中应连接数据库验证
                if ("admin".equals(username) && "admin123".equals(new String(password))) {
                    JOptionPane.showMessageDialog(AdminLoginFrame.this, "登录成功！");
                    // 打开管理员主界面
                    new AdminMainFrame().setVisible(true);
                    dispose(); // 关闭登录窗口
                } else {
                    JOptionPane.showMessageDialog(AdminLoginFrame.this,
                            "用户名或密码错误", "登录失败", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        panel.add(loginButton);

        add(panel);
    }
}
